package com.hamu.mapper;

import com.hamu.pojo.Article;
import com.hamu.pojo.ArticleTag;
import com.hamu.pojo.Tag;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleTagMapper {
    // 检查是否存在任意一个关联
    boolean existsRelation(@Param("articleId") Integer articleId, @Param("tagIds") List<Integer> tagIds);

    // 批量插入关联
    void insertRelations(@Param("articleId") Integer articleId, @Param("tagIds") List<Integer> tagIds);

    // 批量删除关联
    void deleteRelations(@Param("articleId") Integer articleId, @Param("tagIds") List<Integer> tagIds);

    //删除所有关联
    void deleteAllRelationsByArticleId(@Param("articleId") Integer articleId);

    // 新增是否存在关联校验
    boolean existsRelationByArticleId(@Param("articleId") Integer articleId);

    // 查询标签是否存在
    @Select("<script>" +
            "SELECT * FROM tags WHERE tag_id IN " +
            "<foreach item='id' collection='tagIds' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Tag> findTagsByIds(@Param("tagIds") List<Integer> tagIds);

    // 根据标签查询文章
    List<Article> selectArticlesByTag(
            @Param("tagNames") List<String> tagNames,
            @Param("tagIds") List<Integer> tagIds);
    @Select("SELECT COUNT(*) FROM article_tag WHERE tag_id = #{tagId}")
    int countByTagId(Integer tagId);
}